using System;
using System.Collections.Generic;
using System.Text;

using RecruitmentSystem.Common;
using RecruitmentSystem.DataAccess;

namespace RecruitmentSystem.Business
{
    public class AccountManager
    {
        private UserAccessor userAccessor;

        public AccountManager()
        {
            userAccessor = new UserAccessor();
        }

        public bool CreateUser(User user)
        {
            try
            {
                if (user is PersonalUser)
                {
                    userAccessor.AddPersonalUser(user as PersonalUser);
                }
                else if (user is EnterpriseUser)
                {
                    userAccessor.AddEnterpriseUser(user as EnterpriseUser);
                }
                else if (user is SchoolUser)
                {
                    userAccessor.AddSchoolUser(user as SchoolUser);
                }
                else
                {
                    // Nothing to do.
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in register user: ", ex);
                return false;
            }

            return true;
        }

        public bool ChangePassword(string newPassword,
            string oldPassword, string username, UserType userType)
        {
            bool result = false;

            try
            {
                switch (userType)
                {
                    case UserType.AdminUser:
                        result = userAccessor.UpdatePasswordForAdminUser(newPassword,
                            oldPassword, username);
                        break;
                    case UserType.EnterpriseUser:
                        result =  userAccessor.UpdatePasswordForEnterpriseUser(newPassword,
                            oldPassword, username);
                        break;
                    case UserType.PersonalUser:
                        result = userAccessor.UpdatePasswordForPersonalUser(newPassword,
                            oldPassword, username);
                        break;
                    case UserType.SchoolUser:
                        result = userAccessor.UpdatePasswordForSchoolUser(newPassword,
                            oldPassword, username);
                        break;
                    default:
                        return false;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in change password", ex);
            }

            return result;
        }

        public bool UpdateUser(User user)
        {
            bool result = false;

            try
            {
                if (user is PersonalUser)
                {
                    result = userAccessor.UpdatePersonalUser(user as PersonalUser);
                }
                else if (user is EnterpriseUser)
                {
                    result = userAccessor.UpdateEnterpriseUser(user as EnterpriseUser);
                }
                else if (user is SchoolUser)
                {
                    result = userAccessor.UpdateSchoolUser(user as SchoolUser);
                }
                else
                {
                    // Nothing to do.
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in update user", ex);
            }

            return result;
        }

        public bool CheckUserId(string username, UserType userType)
        {
            bool result = false;

            try
            {
                switch (userType)
                {
                    case UserType.AdminUser:
                        break;
                    case UserType.EnterpriseUser:
                        result = userAccessor.CheckEnterpriseUserId(username);
                        break;
                    case UserType.SchoolUser:
                        result = userAccessor.CheckSchoolUserId(username);
                        break;
                    case UserType.PersonalUser:
                        result = userAccessor.CheckPersonalUserId(username);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in check user id", ex);
            }

            return result;
        }

        public User GetUserById(string username, UserType userType)
        {
            User user = null;

            switch (userType)
            {
                case UserType.AdminUser:
                    user = userAccessor.GetAdminUserById(username);
                    break;
                case UserType.EnterpriseUser:
                    user = userAccessor.GetEnterpriseUserById(username);
                    break;
                case UserType.PersonalUser:
                    user = userAccessor.GetPersonalUserById(username);
                    break;
                case UserType.SchoolUser:
                    user = userAccessor.GetSchoolUserById(username);
                    break;
                default:
                    break;
            }

            return user;
        }

        public User GetUserById(int userId, UserType userType)
        {
            User user = null;

            switch (userType)
            {
                case UserType.AdminUser:
                    break;
                case UserType.EnterpriseUser:
                    user = userAccessor.GetEnterpriseUserById(userId);
                    break;
                case UserType.PersonalUser:
                    user = userAccessor.GetPersonalUserById(userId);
                    break;
                case UserType.SchoolUser:
                    user = userAccessor.GetSchoolUserById(userId);
                    break;
                default:
                    break;
            }

            return user;
        }

        public bool RemoveUser(string username, UserType userType)
        {
            bool result = false;

            try
            {
                switch (userType)
                {
                    case UserType.AdminUser:
                        break;
                    case UserType.EnterpriseUser:
                        result = userAccessor.DeleteEnterpriseUser(username);
                        break;
                    case UserType.SchoolUser:
                        result = userAccessor.DeleteSchoolUser(username);
                        break;
                    case UserType.PersonalUser:
                        result = userAccessor.DeletePersonalUser(username);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in remove user", ex);
            }

            return result;
        }

        public bool RemoveUser(int userId, UserType userType)
        {
            bool result = false;

            try
            {
                switch (userType)
                {
                    case UserType.AdminUser:
                        break;
                    case UserType.EnterpriseUser:
                        result = userAccessor.DeleteEnterpriseUser(userId);
                        break;
                    case UserType.SchoolUser:
                        //result = userAccessor.DeleteSchoolUser(userId);
                        break;
                    case UserType.PersonalUser:
                        //result = userAccessor.DeletePersonalUser(userId);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in remove user", ex);
            }

            return result;
        }

        public bool RemoveUser(List<int> ids, UserType userType)
        {
            bool result = false;

            try
            {
                foreach (int id in ids)
                {
                    RemoveUser(id, userType);
                }

                result = true;
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in remove user", ex);
            }

            return result;
        }

        public bool ChangeUserStatus(int userId, UserStatus userStatus, UserType userType)
        {
            bool result = false;

            try
            {
                switch (userType)
                {
                    case UserType.EnterpriseUser:
                        result = userAccessor.UpdateEnterpriseUserStatus(userId, userStatus);
                        break;
                    case UserType.SchoolUser:
                        result = userAccessor.UpdateSchoolUserStatus(userId, userStatus);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in change user status", ex);
            }

            return result;
        }

        public bool ChangeUserStatus(List<int> ids, UserStatus userStatus, UserType userType)
        {
            bool result = false;

            try
            {
                foreach (int id in ids)
                {
                    ChangeUserStatus(id, userStatus, userType);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in change user status", ex);
            }

            return result;
        }

        public UserStatus GetUserStatus(string username, UserType userType)
        {
            UserStatus userStatus = UserStatus.Unapproved;

            try
            {
                switch (userType)
                {
                    case UserType.EnterpriseUser:
                        userStatus = (UserStatus)userAccessor.GetEnterpriseUserStatus(username);
                        break;
                    case UserType.SchoolUser:
                        userStatus = (UserStatus)userAccessor.GetSchoolUserStatus(username);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get user status", ex);
            }

            return userStatus;
        }

        public int GetUserId(string username, UserType userType)
        {
            int userId = 0;

            try
            {
                switch (userType)
                {
                    case UserType.AdminUser:
                        break;
                    case UserType.EnterpriseUser:
                        userId = userAccessor.GetEnterpriseUserId(username);
                        break;
                    case UserType.SchoolUser:
                        userId = userAccessor.GetSchoolUserId(username);
                        break;
                    case UserType.PersonalUser:
                        userId = userAccessor.GetPersonalUserId(username);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get user id", ex);
            }

            return userId;
        }

        public List<User> GetUsers(UserType userType,
            UserStatus userStatus, int pagesize, int pagecurrent)
        {
            List<User> users = new List<User>();

            try
            {
                switch (userType)
                {
                    case UserType.AdminUser:
                        break;
                    case UserType.EnterpriseUser:
                        users = userAccessor.GetEnterpriseUsers(userStatus, pagesize, pagecurrent);
                        break;
                    case UserType.PersonalUser:
                        //users = userAccessor.GetPersonalUserById(username);
                        break;
                    case UserType.SchoolUser:
                        users = userAccessor.GetSchoolUsers(userStatus, pagesize, pagecurrent);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get users", ex);
            }

            return users;
        }

        public int GetUsersCount(UserType userType, UserStatus userStatus)
        {
            int count = 0;

            try
            {
                switch (userType)
                {
                    case UserType.AdminUser:
                        break;
                    case UserType.EnterpriseUser:
                        count = userAccessor.GetEnterpriseUsersCount(userStatus);
                        break;
                    case UserType.PersonalUser:
                        //users = userAccessor.GetPersonalUserById(username);
                        break;
                    case UserType.SchoolUser:
                        count = userAccessor.GetSchoolUsersCount(userStatus);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get user count", ex);
            }

            return count;
        }

        public bool ChangeEnterpriseType(int userId, EnterpriseType type)
        {
            bool result = false;

            try
            {
                result = userAccessor.UpdateEnterpriseType(userId, Convert.ToInt32(type));
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in change enterprise type", ex);
            }

            return result;
        }

        public bool ChangeEnterpriseLogo(string username, string url)
        {
            bool result = false;

            try
            {
                result = userAccessor.UpdateEnterpriseLogo(username, url);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in change enterprise logo", ex);
            }

            return result;
        }

        public bool ChangePersonalHead(string username, string url)
        {
            bool result = false;

            try
            {
                result = userAccessor.UpdatePersonalHead(username, url);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in change personal head pic", ex);
            }

            return result;
        }

        public List<EnterpriseUser> GetEnterpriseUsers(EnterpriseType type, int count)
        {
            List<EnterpriseUser> users = new List<EnterpriseUser>();

            try
            {
                switch (type)
                {
                    case EnterpriseType.Famous:
                        users = userAccessor.GetFamousEnterprise(count);
                        break;
                    case EnterpriseType.Hot:
                        users = userAccessor.GetHotEnterprise(count);
                        break;
                    case EnterpriseType.Genernal:
                        users = userAccessor.GetGenernalEnterprise(count);
                        break;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get enterprise users", ex);
            }

            return users;
        }
    }
}
